package 逆变协变;

import java.util.ArrayList;
import java.util.List;

/**
 * @author juntao zhang
 */
public class Test {

  static class Person {
    public void hi() {
      System.out.println("hi");
    }
  }

  static class Student extends Person {
    public void hello() {
      System.out.println("hello");
    }
  }

  /*
   要从泛型类取数据时，用extends；
   要往泛型类写数据时，用super；
   既要取又要写，就不用通配符
   */
  public static void main(String[] args) {
    Student[] students = new Student[2];
    Person[] peoples = students;//发生协变
    peoples[0] = new Student();
    peoples[1] = new Person();//运行异常 ArrayStoreException

    //java 在协变逆变很不清楚
    List<? super Student> list = new ArrayList<Person>();
    list.add(new Student());  //逆变
    //list.get(0).hi(); //compile error

//    List<Person> list3 = new ArrayList<Student>();//incompatible error
    List<? extends Person> list2 = new ArrayList<Student>(){{
      add(new Student());
    }};
    //list2.get(0).hello();//compile error
    list2.get(0).hi();




  }
}
